$(function () {

    var client_datagrid = $("#client_datagrid");
    var client_add_dialog = $("#client_add_dialog");
    var client_add_form = $("#client_add_form");
    var client_check_dialog = $("#client_check_dialog");
    var client_check_form = $("#client_check_form");
    var client_trace_dialog = $("#client_trace_dialog");
    var client_trace_form = $("#client_trace_form");
    var client_formal_dialog = $("#client_formal_dialog");
    var client_formal_form = $("#client_formal_form");
    var client_advanced_search_dialog = $("#client_advanced_search_dialog");
    var client_advanced_search_form = $("#client_advanced_search_form");

    // 方法对象,统一管理所有方法
    var cmdObj = {
        // 点击弹开客户添加框
        addClient: function () {
            client_add_form.form('clear');
            client_add_dialog.dialog('setTitle', '添加客户');
            client_add_dialog.dialog('open');
        },
        // 提交
        add_submit: function () {
            var url = '/client/save';
            var client_id = $("#client_id").val();
            if (client_id) {
                url = '/client/update'
            }
            $.messager.confirm('提示', '确定要提交吗?', function (ret) {
                if (ret) {
                    client_add_form.form('submit', {
                        url: url,
                        success: function (data) {
                            data = $.parseJSON(data);
                            if (data.success) {
                                $.messager.alert('提示', '操作成功', 'info');
                                cmdObj.add_cancel();
                                client_datagrid.datagrid('reload');
                            } else {
                                cmdObj.add_cancel();
                                $.messager.alert('提示', data.msg, 'info');
                            }
                        }
                    });
                }
            });
        },
        // 关闭添加客户框
        add_cancel: function () {
            client_add_dialog.dialog('close');
        },
        // 编辑客户
        editClient: function () {
            var currentRow = client_datagrid.datagrid('getSelected');
            if (!currentRow) {
                $.messager.alert('提示', '请选择一行数据', 'error');
                return;
            }
            client_add_form.form('clear');

            if (currentRow.gender) {
                currentRow['gender.id'] = currentRow.gender.id;
            }

            if (currentRow.wishLevel) {
                currentRow['wishLevel.id'] = currentRow.wishLevel.id;
            }

            if (currentRow.wishCampus) {
                currentRow['wishCampus.id'] = currentRow.wishCampus.id;
            }

            if (currentRow.wishClass) {
                currentRow['wishClass.id'] = currentRow.wishClass.id;
            }

            if (currentRow.wishSubject) {
                currentRow['wishSubject.id'] = currentRow.wishSubject.id;
            }

            if (currentRow.clientType) {
                currentRow['clientType.id'] = currentRow.clientType.id;
            }

            if (currentRow.bigCustomer) {
                currentRow['bigCustomer.id'] = currentRow.bigCustomer.id;
            }

            if (currentRow.foreignLangLevel) {
                currentRow['foreignLangLevel.id'] = currentRow.foreignLangLevel.id;
            }

            if (currentRow.origin) {
                currentRow['origin.id'] = currentRow.origin.id;
            }

            /* if (currentRow.haspaid) {
             console.log(currentRow.haspaid);
             }

             if (currentRow.isTrace) {
             $("#isTrace_combobox").combobox("select", "1");
             } else {
             $("#isTrace_combobox").combobox("select", "0");
             }
             */
            client_add_form.form('load', currentRow);
            client_add_dialog.dialog('setTitle', '编辑客户');
            client_add_dialog.dialog('open');
        },
        // 刷新页面
        reloadClient: function () {
            $("#keyword_input").textbox('clear');
            client_advanced_search_form.form('clear');
            client_datagrid.datagrid('reload');
        },
        // 查看详情
        detailsClient: function () {
            var currentRow = client_datagrid.datagrid('getSelected');
            if (!currentRow) {
                $.messager.alert('提示', '请选择一行数据', 'error');
                return;
            }

            client_check_form.form('clear');

            client_check_form.form('disableValidation')

            if (currentRow.gender) {
                currentRow['gender.id'] = currentRow.gender.id;
            }

            if (currentRow.wishLevel) {
                currentRow['wishLevel.id'] = currentRow.wishLevel.id;
            }

            if (currentRow.wishCampus) {
                currentRow['wishCampus.id'] = currentRow.wishCampus.id;
            }

            if (currentRow.wishClass) {
                currentRow['wishClass.id'] = currentRow.wishClass.id;
            }

            if (currentRow.wishSubject) {
                currentRow['wishSubject.id'] = currentRow.wishSubject.id;
            }

            if (currentRow.clientType) {
                currentRow['clientType.id'] = currentRow.clientType.id;
            }

            if (currentRow.bigCustomer) {
                currentRow['bigCustomer.id'] = currentRow.bigCustomer.id;
            }

            if (currentRow.foreignLangLevel) {
                currentRow['foreignLangLevel.id'] = currentRow.foreignLangLevel.id;
            }

            if (currentRow.inputEmployee) {
                currentRow['inputEmployee.id'] = currentRow.inputEmployee.id;
            }

            client_check_form.form('load', currentRow);

            // 将表单设置为只读
            $("#client_check_form").find(".easyui-combobox").combobox('readonly', true);
            $("#client_check_form").find(".easyui-datebox").datebox('readonly', true);
            $("#client_check_form").find(".easyui-textbox").textbox('readonly', true);

            /* $(".easyui-combobox").combobox('readonly', true);
             $(".easyui-datebox").datebox('readonly', true);
             $(".easyui-textbox").textbox('readonly', true);*/

            client_check_dialog.dialog('setTitle', '查看详情');
            client_check_dialog.dialog('open');
        },
        // 查看详情确认
        check_ok: function () {
            client_check_dialog.dialog('close');
        },
        // 跟进客户
        traceClient: function () {
            var currentRow = client_datagrid.datagrid('getSelected');
            if (!currentRow) {
                $.messager.alert('提示', '请选择一行数据', 'error');
                return;
            }

            client_trace_form.form('clear');

            if (currentRow.gender) {
                currentRow['gender.id'] = currentRow.gender.id;
            }

            if (currentRow.foreignLangLevel) {
                currentRow['foreignLangLevel.id'] = currentRow.foreignLangLevel.id;
            }

            client_trace_form.form('load', currentRow);
            client_trace_dialog.dialog('setTitle', '跟进客户');
            // 将当前行的客户id赋值给clientId
            $("#clientId").val(currentRow.id);
            client_trace_dialog.dialog('open');

        },
        // 跟进提交
        trace_submit: function () {

            var currentRow = client_datagrid.datagrid('getSelected');
            var clientId = currentRow.id;
            $.post('/client/judge', {clientId: clientId}, function (data) {
                if (data.success) {
                    $.messager.confirm('提示', '确定要提交吗?', function (ret) {
                        if (ret) {
                            client_trace_form.form('submit', {
                                url: '/trace/traceClient',
                                success: function (data) {
                                    data = $.parseJSON(data);
                                    if (data.success) {
                                        $.messager.alert('提示', '操作成功', 'info');
                                        cmdObj.trace_cancel();
                                        client_datagrid.datagrid('reload');
                                    } else {
                                        $.messager.alert('提示', data.msg, 'error');
                                    }
                                }
                            });
                        }
                    });
                } else {
                    cmdObj.trace_cancel();
                    $.messager.alert('提示', data.msg, 'error');
                }
            });
        },
        // 跟进取消
        trace_cancel: function () {
            client_trace_dialog.dialog('close');
        },
        // 共享到资源池
        shareClient: function () {
            var currentRow = client_datagrid.datagrid('getSelected');
            if (!currentRow) {
                $.messager.alert('提示', '请选择一行数据', 'error');
                return;
            }
            var clientId = currentRow.id;

            $.post('/client/judge', {clientId: clientId}, function (data) {

                if (!data.success) {
                    $.messager.alert('提示', data.msg, 'info');
                    cmdObj.trace_cancel();
                } else {
                    $.messager.confirm('提示', '确定要将客户转入资源池吗?', function (ret) {
                        if (ret) {
                            $.post('/client/toPool', {clientId: clientId}, function (data) {
                                if (data.success) {
                                    $.messager.alert('提示', '操作成功', 'info');
                                    cmdObj.trace_cancel();
                                    client_datagrid.datagrid('reload');
                                }
                            });
                        } else {
                            $.messager.alert('提示', data.msg, 'info');
                        }
                    });
                }
            });
        },
        // 将客户转为正式学员
        formalClient: function () {
            var currentRow = client_datagrid.datagrid('getSelected');
            if (!currentRow) {
                $.messager.alert('提示', '请选择一行数据', 'error');
                return;
            }
            var clientId = currentRow.id;
            $.post('/client/judge', {clientId: clientId}, function (data) {
                if (data.success) {
                    client_formal_form.form('clear');
                    if (currentRow.inputEmployee) {
                        currentRow['inputEmp'] = currentRow.inputEmployee.username;
                    }

                    client_formal_form.form('load', currentRow);

                    if (currentRow.gender) {
                        currentRow.gender.id == 18 ? $("#gender").combobox('setValue', 0) : $("#gender").combobox('setValue', 1)
                    } else {
                        $("#gender").combobox('setValue', 0);
                    }

                    client_formal_dialog.dialog('open');
                } else {
                    $.messager.alert('提示', data.msg, 'info');
                }
            });


            /*client_formal_form.form('clear');

             if (currentRow.gender) {
             currentRow['gender.id'] = currentRow.gender.id;
             }
             if (currentRow.inputEmployee) {
             currentRow['inputEmployee.id'] = currentRow.inputEmployee.username;
             }

             client_formal_form.form('load', currentRow);
             client_formal_dialog.dialog('open');*/
        },
        // 潜在学员转正提交
        formal_submit: function () {
            var currentRow = client_datagrid.datagrid('getSelected');
            var clientId = currentRow.id;
            $.messager.confirm('提示', '客户确定入学吗?', function (ret) {
                if (ret) {
                    client_formal_form.form('submit', {
                        url: '/client/formal',
                        /*onSubmit: function (param) {
                         param.idNum = $("#idNum").val();
                         param.emergencyName = $("#idNum").val();
                         param.emergencyTel = $("#idNum").val();
                         param.wishWork = $("#idNum").val();
                         param.experienceWork = 'value2';
                         },*/
                        success: function (data) {
                            data = $.parseJSON(data);
                            if (data.success) {
                                $.post('/client/removeClient', {clientId: clientId}, function () {
                                });
                                $.messager.alert('提示', '操作成功', 'info');
                                cmdObj.formal_cancel();
                                client_datagrid.datagrid('reload');
                            } else {
                                $.messager.alert('提示', data.msg, 'error');
                            }
                        }
                    });
                }
            });
        },
        // 潜在学员转正取消
        formal_cancel: function () {
            client_formal_dialog.dialog('close');
        },
        // 高级查询条件框打开
        advancedSearchClient: function () {
            client_advanced_search_form.form('clear');
            client_advanced_search_dialog.dialog('open');
        },
        // 高级查询条件框提交
        advanced_submit: function () {
            var clientName = $("#a_clientName").val();
            var a_gender = $("#a_gender").val();
            var a_minAge = $("#a_minAge").val();
            var a_maxAge = $("#a_maxAge").val();
            var a_beginTime = $("#a_beginTime").val();
            var a_endTime = $("#a_endTime").val();
            var a_wishLevel = $("#a_wishLevel").val();
            var a_wishSubject = $("#a_wishSubject").val();
            var a_wishCampus = $("#a_wishCampus").val();
            var a_wishClass = $("#a_wishClass").val();

            client_datagrid.datagrid('load', {
                'clientName': clientName,
                'beginTime': a_beginTime,
                'endTime': a_endTime,
                'minAge': a_minAge,
                'maxAge': a_maxAge,
                'gender': a_gender,
                'wishLevel': a_wishLevel,
                'wishCampus': a_wishCampus,
                'wishClass': a_wishClass,
                'wishSubject': a_wishSubject
            });
            client_advanced_search_dialog.dialog('close');
        },
        // 高级查询条件框取消
        advanced_cancel: function () {
            client_advanced_search_dialog.dialog('close');
        },
        // 点击方法镜查找
        confirmSearch: function () {
            var keyword = $("#keyword_input").val();
            client_datagrid.datagrid('load', {
                'keyword': keyword
            })
            $("#keyword_input").textbox('clear');
        },
        // 导出
        exportClient: function () {
            $.messager.confirm('提示', '是否确定要导出潜在客户表?', function (ret) {
                if (ret) {
                    window.location.href = '/client/export';
                }
            });
        }
    }

    // 获取页面所有按钮,统一方法调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });


    /*高级查询条件框*/
    client_advanced_search_dialog.dialog({
        title: '高级查询条件',
        buttons: '#client_advanced_search_dialog_buttons',
        closed: true
    });

    /*转正框*/
    client_formal_dialog.dialog({
        title: '客户入学登记',
        width: 803,
        height: 520,
        closed: true,
        border: 'thin',
        buttons: '#client_formal_buttons'
    });

    client_datagrid.datagrid({
        url: '/client/query',
        fit: true,
        fitColumns: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        border: false,
        openAnimation: 'show',
        width: 1000,
        columns: [[
            {field: 'id', title: '编号', halign: 'center', align: 'center', width: 100, hidden: true},
            {field: 'name', title: '客户名', halign: 'center', align: 'center', width: 100},
            {field: 'qq', title: 'qq号码', halign: 'center', align: 'center', width: 100},
            {field: 'tel', title: '电话', halign: 'center', align: 'center', width: 100},
            {field: 'email', title: '邮箱', halign: 'center', align: 'center', width: 100},
            {field: 'wechat', title: '微信', halign: 'center', align: 'center', width: 100},
            {
                field: 'inputEmployee', title: '录入人', halign: 'center', align: 'center', width: 100,
                formatter: formatInputEmployee
            },
            {field: 'buildDocTime', title: '建档时间', halign: 'center', align: 'center', width: 100},
            {field: 'traceNumber', title: '跟进次数', halign: 'center', align: 'center', width: 100},
            {field: 'lastTraceTime', title: '最后跟进时间', halign: 'center', align: 'center', width: 100},
            {
                field: 'wishLevel', title: '意向等级', halign: 'center', align: 'center', width: 100,
                formatter: formatWishLevel
            },
            {
                field: 'wishCampus', title: '意向校区', halign: 'center', align: 'center', width: 100,
                formatter: formatWishCampus
            },
            {
                field: 'wishClass', title: '意向班级', halign: 'center', align: 'center', width: 100,
                formatter: formatWishClass
            },
            {
                field: 'wishSubject', title: '意向学科', halign: 'center', align: 'center', width: 100,
                formatter: formatWishSubject
            },
            {
                field: 'state', title: '状态', halign: 'center', align: 'center', width: 100,
                formatter: formatState
            },
            {
                field: 'attachEmployee', title: '当前跟进人', halign: 'center', align: 'center', width: 100,
                formatter: formatAttachEmployee
            },
            {field: 'remark', title: '备注', halign: 'center', align: 'center', width: 100}
        ]],
        toolbar: '#client_datagrid_toolbar'
    });

});

// 格式化录入人
function formatInputEmployee(value, row, index) {
    return value == null ? '' : value.username;
}
// 格式化当前跟进人
function formatAttachEmployee(value, row, index) {
    return value == null ? '' : value.username;
}
// 格式化意向等级
function formatWishLevel(value, row, index) {
    return value == null ? '' : value.name;
}
// 格式化意向校区
function formatWishCampus(value, row, index) {
    return value == null ? '' : value.name;
}
// 格式化意向班级
function formatWishClass(value, row, index) {
    return value == null ? '' : value.name;
}
// 格式化意向学科
function formatWishSubject(value, row, index) {
    return value == null ? '' : value.name;
}
// 格式化状态
function formatState(value, row, index) {
    return value == null ? '' : "<span style='color: orange'>" + value.name + "</span>";
}
// 格式化跟进
function formatTrace(value, row, index) {
    return value ? '跟进中' : '未跟进';
}

$.extend($.fn.validatebox.defaults.rules, {
    minLength: {
        validator: function (value, param) {
            return value.length >= param[0];
        },
        message: '请按提示输入字符数量'
    },
    telNum: { //既验证手机号，又验证座机号
        validator: function (value, param) {
            return /(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\d3)|(\d{3}\-))?(1[358]\d{9})$)/.test(value);
        },
        message: '请输入正确的手机/固话号码。'
    },
    numberCheck: {
        validator: function (value) {
            return /^[0-9]+$/.test(value);
        },
        message: "只能输入数字"
    },
    englishCheckSub: {
        validator: function (value) {
            return /^[a-zA-Z0-9]+$/.test(value);
        },
        message: "只能包括英文字母、数字"
    },
    idcard: {// 验证身份证
        validator: function (value) {
            return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
        },
        message: '身份证号码格式不正确'
    },
    CHS: {
        validator: function (value) {
            return /^[\u0391-\uFFE5]+$/.test(value);
        },
        message: "只能输入汉字"
    },
});


function isExsists() {
    var stuName = $(this).val();
    if (stuName.length >= 2) {
        $.post('/regularStu/isExsists', {stuName: stuName}, function (data) {
            if (!data.success) {
                $.messager.alert('提示', data.msg, 'info');
                $('#introducer_input').val('');
            }
        });
    }
}

function checkClient() {
    var clientName = $(this).val();
    var clientId = $("#client_id").val();
    if (clientName != null) {
        $.post('/client/isExsists', {clientName: clientName, clientId: clientId}, function (data) {
            if (!data.success) {
                $.messager.alert('提示', data.msg, 'info');
                $('#cName').val('');
            }
        });
    }
}


function count() {
    var totalFee = $(this).val();
    var paidFee = $("#paidFee").val()
    var surplus = totalFee - paidFee

    surplus = surplus < 0 ? 0 : surplus;

    if (surplus) {
        $("#feeStatus").combobox('select', 0);
    } else {
        $("#feeStatus").combobox('select', 1);
    }
    var unPaidFee = $("#unPaidFee");

    unPaidFee.textbox('setText', surplus);
}
function count1() {
    var paidFee = $(this).val();
    var totalFee = $("#totalFee").val()
    var surplus = totalFee - paidFee

    surplus = surplus < 0 ? 0 : surplus;

    if (surplus) {
        $("#feeStatus").combobox('select', 0);
    } else {
        $("#feeStatus").combobox('select', 1);
    }
    var unPaidFee = $("#unPaidFee");

    unPaidFee.textbox('setText', surplus);
}






















